package com.ssnoodles.weathersimple.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ssnoodles.weathersimple.model.City;
import com.ssnoodles.weathersimple.model.County;
import com.ssnoodles.weathersimple.model.Province;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ssnoodles on 2016/10/17.
 */

public class WeatherDb {

    public static final int VERSION = 1;

    private SQLiteDatabase db;

    private static WeatherDb weatherDb;

    private WeatherDb(Context context) {
        DbHelper dbHelper = new DbHelper(context, DbHelper.DB_NAME, null, VERSION);
        db = dbHelper.getWritableDatabase();
    }

    /**
     * 获取 实例
     * @param context
     * @return
     */
    public synchronized static WeatherDb getInstance(Context context) {
        if (weatherDb == null) {
            weatherDb = new WeatherDb(context);
        }
        return weatherDb;
    }

    /**
     * 从数据库读取全国所有的省份信息。
     */
    public List<Province> loadProvinces() {
        List<Province> list = new ArrayList<Province>();
        Cursor cursor = db.query("Province", null, null, null, null, null, "province_code asc");
        if (cursor.moveToFirst()) {
            do {
                Province province = new Province();
                province.setId(cursor.getInt(cursor.getColumnIndex("id")));
                province.setProvinceName(cursor.getString(cursor
                        .getColumnIndex("province_name")));
                province.setProvinceCode(cursor.getString(cursor.getColumnIndex("province_code")));
                list.add(province);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return list;
    }

    /**
     * 从数据库读取某省下所有的城市信息。
     */
    public List<City> loadCities(String provinceCode) {
        List<City> list = new ArrayList<City>();
        Cursor cursor = db.query("City", null, "province_code = ?",
                new String[] { provinceCode }, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                City city = new City();
                city.setId(cursor.getInt(cursor.getColumnIndex("id")));
                city.setCityName(cursor.getString(cursor
                        .getColumnIndex("city_name")));
                city.setCityCode(cursor.getString(cursor
                        .getColumnIndex("city_code")));
                city.setProvinceCode(provinceCode);
                list.add(city);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return list;
    }

    /**
     * 从数据库读取某城市下所有的县信息。
     */
    public List<County> loadCounties(String cityCode) {
        List<County> list = new ArrayList<County>();
        Cursor cursor = db.query("County", null, "city_code = ?",
                new String[] { cityCode }, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                County county = new County();
                county.setId(cursor.getInt(cursor.getColumnIndex("id")));
                county.setCountyName(cursor.getString(cursor
                        .getColumnIndex("county_name")));
                county.setCountyCode(cursor.getString(cursor
                        .getColumnIndex("county_code")));
                county.setCityCode(cityCode);
                list.add(county);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return list;
    }




}
